# Maintainer: Antoine Martin <totaam@xpra.org>

_realname=xpra
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}"
           "${MINGW_PACKAGE_PREFIX}-${_realname}-common")
replaces=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}"
          "${MINGW_PACKAGE_PREFIX}-${_realname}-common")
provides=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=4.3.4
pkgrel=1
pkgdesc='Remote access client/server software (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url='https://xpra.org'
license=('spdx:GPL-2.0-or-later')
depends=(
    ${MINGW_PACKAGE_PREFIX}-ffmpeg
    ${MINGW_PACKAGE_PREFIX}-gtk3
    ${MINGW_PACKAGE_PREFIX}-libjpeg-turbo
    ${MINGW_PACKAGE_PREFIX}-libvpx
    ${MINGW_PACKAGE_PREFIX}-libwebp
    ${MINGW_PACKAGE_PREFIX}-libyuv
    ${MINGW_PACKAGE_PREFIX}-python
    ${MINGW_PACKAGE_PREFIX}-python-comtypes
    ${MINGW_PACKAGE_PREFIX}-python-lz4
    ${MINGW_PACKAGE_PREFIX}-python-pillow
    ${MINGW_PACKAGE_PREFIX}-python-pyopengl
    ${MINGW_PACKAGE_PREFIX}-python-rencode
    ${MINGW_PACKAGE_PREFIX}-python-setproctitle
    ${MINGW_PACKAGE_PREFIX}-x264)
optdepends=(
    "${MINGW_PACKAGE_PREFIX}-libnotify: notification support"
    "${MINGW_PACKAGE_PREFIX}-gst-plugins-bad: extra audio codecs"
    "${MINGW_PACKAGE_PREFIX}-gst-plugins-ugly: extra audio codecs"
    "${MINGW_PACKAGE_PREFIX}-gst-plugins-good: extra audio codecs"
    "${MINGW_PACKAGE_PREFIX}-gst-python: audio support"
    "${MINGW_PACKAGE_PREFIX}-python-paramiko: SSH client and server support"
    "${MINGW_PACKAGE_PREFIX}-python-dnspython: SSHFP support with paramiko"
    "${MINGW_PACKAGE_PREFIX}-python-netifaces: mDNS and network integration"
    "${MINGW_PACKAGE_PREFIX}-python-pyu2f: U2F authentication"
    "${MINGW_PACKAGE_PREFIX}-python-ldap: LDAP authentication via python-ldap"
    "${MINGW_PACKAGE_PREFIX}-python-ldap3: LDAP authentication via python-ldap3"
    "${MINGW_PACKAGE_PREFIX}-python-cryptography: AES packet encryption"
    "${MINGW_PACKAGE_PREFIX}-python-zeroconf: mDNS support"
    "${MINGW_PACKAGE_PREFIX}-python-nvidia-ml: nvidia GPU support")
makedepends=(
    ${MINGW_PACKAGE_PREFIX}-brotli
    ${MINGW_PACKAGE_PREFIX}-cython
    ${MINGW_PACKAGE_PREFIX}-cc
    ${MINGW_PACKAGE_PREFIX}-pkg-config
    ${MINGW_PACKAGE_PREFIX}-python-cairo
    ${MINGW_PACKAGE_PREFIX}-python-setuptools
    ${MINGW_PACKAGE_PREFIX}-pygobject-devel)
source=("https://xpra.org/src/${_realname}-${pkgver}.tar.xz"
        "${_realname}-${pkgver}.tar.xz.asc::https://xpra.org/src/${_realname}-${pkgver}.tar.xz.gpg")
sha512sums=('9914d15957c3d61792cddda008d21135f795bf0b9579a42d78add53b85a35f5ac9e0db0063d7e161b7a9fab9da8a6a5722d65ecfc1b5457b36cc57ffed8400a1'
            'SKIP')
validpgpkeys=('C11C0A4DF702EDF6C04F458C18ADB31CF18AD6BB') # Antoine Martin <antoine@nagafix.co.uk>

case ${MSYSTEM} in
  CLANG*)
    CFLAGS="$CFLAGS -Wno-error=unreachable-code -Wno-error=sometimes-uninitialized";;
esac

prepare() {
  cd "${srcdir}"
  rm -rf python-build-${MSYSTEM} | true
  cp -r "${_realname}-${pkgver}" "python-build-${MSYSTEM}"
}

build() {
  msg "Python build for ${MSYSTEM}"
  cd "${srcdir}/python-build-${MSYSTEM}"

  CFLAGS+=" -Wno-error=address"  # triggers gcc 12 error in some auto generated cython code
  ${MINGW_PREFIX}/bin/python setup.py build \
    --without-cuda_kernels --with-data --without-docs --with-modules
}

package() {
  cd "${srcdir}/python-build-${MSYSTEM}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python setup.py install --prefix=${MINGW_PREFIX} \
    --without-cuda_kernels --with-data  --without-docs \
    --root="${pkgdir}" --share-xpra="./share/xpra" --optimize=1 --skip-build

  install -Dm644 COPYING "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/COPYING"
  install -Dm644 README.md "${pkgdir}${MINGW_PREFIX}/share/${_realname}/README.md"
  install -Dm644 fs/share/xpra/bell.wav "${pkgdir}${MINGW_PREFIX}/share/${_realname}/bell.wav"

  #python version agnostic launch scripts in /bin:
  for script in xpra xpra_launcher; do
    install -Dm755 fs/bin/${script} "${pkgdir}${MINGW_PREFIX}/bin/${script}"
    #patch path to the python interpreter:
    sed -i -e "s+/usr/bin/python+${MINGW_PREFIX}/bin/python+g" "${pkgdir}${MINGW_PREFIX}/bin/${script}"
  done
}
